package com.meessiom.train.crm.user.controller;

import com.meessiom.train.crm.customer.model.Customer;
import com.meessiom.train.crm.user.model.User;
import com.meessiom.train.crm.user.repository.UserRepository;
import com.meessiom.train.crm.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping("")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserRepository userRepository;
    @GetMapping
    public List<User> getAllUsers() {return userService.getAllUser();}
    @PostMapping
    public User cresteUser(@RequestBody User user){return userService.
    createUser(user);}

    @GetMapping("{id}")

    public ResponseEntity<User> getUserById(@PathVariable long id){
       User user =userService.getUserById(id);
        return ResponseEntity.ok(user);
    }
    @GetMapping("/findByNameAndPhoneAndAddress/")
    public List<User> findUserByNameLikeAndPhoneLikeAndAddressLike (@RequestParam("name") String name,
                                                                    @RequestParam("phone") String phone,
                                                                    @RequestParam("address")String address){
        return userRepository.findUserByUserNameLikeAndUserPhoneLikeAndUserAddressLike("%"+name+"%","%"+phone+"%","%"+address+"%");
    }
    @GetMapping("/getUserNameById/{id}")
    public String getCustomerNameByCustomerId( @PathVariable long id){
        String customerName = userService.getUserNameByUserId(id);
        return customerName;
    }

    @PutMapping("{id}")
    public ResponseEntity<User> deleteUser(@PathVariable long id,@RequestBody User userDetails){
        User updateUser = userService.updateUser(id,userDetails);

        return ResponseEntity.ok(updateUser);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<HttpStatus> deleteUser(@PathVariable long id){
        userService.deleteUser(id);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}


